package com.zzy.service.impl;

import com.zzy.dao.BaseDao;
import com.zzy.model.TestModel;
import com.zzy.service.TestModelService;
import com.zzy.util.UtilJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

@Transactional
@Service()
public class TableServiceImpl implements TestModelService {

    @Autowired
    private BaseDao<TestModel> basedao;

    public List<TestModel> getList(TestModel model) {
        String hql = " from TestModel where 1=1 ";
        Object[] values = null;
        List valuesList = new LinkedList();
        if(model!=null){

            String name = model.getTitle();
            if(StringUtils.hasText(name)){
                hql += " and name like ? ";
                valuesList.add("%"+name+"%");
            }
        }
        values = new Object[valuesList.size()];
        valuesList.toArray(values);
        List<TestModel> list = basedao.find(hql,values);
        return list;
    }

    public List<TestModel> getListByPage(Integer page, Integer rows) {
        return null;
    }

    public TestModel getModelByParam(TestModel model) {
        return null;
    }

    public void delete(String id) {
        if(StringUtils.hasText(id)){
            TestModel model = getByID(id);
            if(model!=null){
                basedao.delete(model);
            }
        }
    }


    public void saveOrUpdate(TestModel model) {
        if(model!=null && StringUtils.hasText(model.getId())){
            basedao.update(model);
        }else{
            basedao.save(model);
        }

    }


    public TestModel getByID(String id) {
        if(StringUtils.hasText(id)){
            return basedao.get(" from TestModel where id = ? ",new Object[]{id});
        }
        return null;
    }

    /**获取总条数*/
    public Integer getCount(){
        return 0;
    }
    public void getList(TestModel model, HttpServletRequest request , HttpServletResponse response) {
        List list = getList(model);
        Integer count = getCount();
        UtilJson.jsonForLayerUI(list,count,response);
    }
}
